package com.moonLight.weblog.admin.event.subscriber;

import com.moonLight.weblog.admin.event.UpdateCommentEvent;
import com.moonLight.weblog.common.domain.dos.BlogSettingsDO;
import com.moonLight.weblog.common.domain.dos.CommentDO;
import com.moonLight.weblog.common.domain.mapper.BlogSettingsMapper;
import com.moonLight.weblog.common.domain.mapper.CommentMapper;
import com.moonLight.weblog.common.enums.CommentStatusEnum;
import com.moonLight.weblog.common.mail.MailHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.omg.CosNaming.NamingContextPackage.NotEmptyHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Slf4j
public class UpdateCommentEventSubscriber implements ApplicationListener<UpdateCommentEvent> {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private MailHelper mailHelper;
    @Autowired
    private BlogSettingsMapper blogSettingsMapper;

    @Override
    public void onApplicationEvent(UpdateCommentEvent event) {

        Long commentId = event.getCommentId();
        CommentDO commentDO = commentMapper.selectById(commentId);
        Integer status = commentDO.getStatus();
        String mail = commentDO.getMail(); // 评论者邮箱
        String content = commentDO.getContent();
        String routerUrl = commentDO.getRouterUrl();
        Long replyCommentId = commentDO.getReplyCommentId();
        BlogSettingsDO blogSettingsDO = blogSettingsMapper.selectById(1L);
        String domain = "http://121.41.168.73/#/";
        String blogName = blogSettingsDO.getName();


        // 判断评论是否审核通过
        if (Objects.equals(status, CommentStatusEnum.NORMAL.getCode()) && StringUtils.isNotBlank(mail)){
            //审核通过
            String title = String.format("你在%s的评论已被审核通过", blogName);
            String html = String.format("<html><body>" +
                            "<h2>你的评论:</h2><p>%s</p>" +
                            "<p><a href='%s%s' target='_blank'>查看详情</a></p>" +
                            "</body></html>",
                    content, domain, routerUrl);
            mailHelper.sendMail(mail, title, html);
            if (Objects.nonNull(replyCommentId)){
                //通知被回复人的邮箱
                extracted(replyCommentId, blogName, content, domain, routerUrl);
            }


        }
        if (Objects.equals(status, CommentStatusEnum.EXAMINE_FAILED.getCode()) && StringUtils.isNotBlank(mail)){
            //审核不通过 并且有邮箱
            String reason = commentDO.getReason(); //原因
            String title = String.format("你在%s的评论未被审核通过", blogName);

            String html = String.format("<html><body>" +
                            "<h2>你的评论:</h2><p>%s</p>" +
                            "<h2>审核未通过原因:</h2><p>%s</p>" +
                            "<p><a href='%s%s' target='_blank'>查看详情</a></p>" +
                            "</body></html>",
                    content, reason, domain, routerUrl);
            mailHelper.sendMail(mail, title, html);
        }




    }

    private void extracted(Long replyCommentId, String blogName, String content, String domain, String routerUrl) {
        //通知被回复人的邮箱
        CommentDO replyCommentDO = commentMapper.selectById(replyCommentId);
        String replyMail = replyCommentDO.getMail();
        String routerUrl1 = replyCommentDO.getRouterUrl();
        String nickname = replyCommentDO.getNickname();
        if (StringUtils.isBlank(replyMail)){
            return;
        }
        String title = String.format("你在%s的评论收到了回复", blogName);

        String html = String.format("<html><body>" +
                        "<h2>你的评论:</h2><p>%s</p>" +
                        "<h2>%s 回复了你:</h2><p>%s</p>" +
                        "<p><a href='%s%s' target='_blank'>查看详情</a></p>" +
                        "</body></html>",
                replyCommentDO.getContent(), nickname, content, domain, routerUrl);
        mailHelper.sendMail(replyMail, title, html);
    }
}
